<?php
    $salesinvoicemail       = $_POST['salesinvoicemail'];
    $accountstatementmail   = $_POST['accountstatementmail'];
    $salesconfirmmail       = $_POST['salesconfirmmail'];
    $contactname            = $_POST['contactname'];

    $accountnum             = $_POST['accountnum'];
    $dataareaid             = $_POST['dataareaid'];
    $recid                  = $_POST['recid'];

    // Tjek om mail er gyldig
    if($salesinvoicemail && !filter_var($salesinvoicemail, FILTER_VALIDATE_EMAIL))
    {
        $salesinvoiceerror = $salesinvoicemail;
    }
    if($accountstatementmail && !filter_var($accountstatementmail, FILTER_VALIDATE_EMAIL))
    {
        $accountstatementmailerror = $accountstatementmail;
    }
    if($salesconfirmmail && !filter_var($salesconfirmmail, FILTER_VALIDATE_EMAIL))
    {
        $salesconfirmmailerror = $salesconfirmmail;
    }

    // create a new XML document
    $doc = new DomDocument('1.0', 'utf-8'); // ISO-8859-1

    // create root node
    $root = $doc->createElement('root');
    $root = $doc->appendChild($root);

        $occ = $doc->createElement('custtable');
        $occ = $root->appendChild($occ);

            $child = $doc->createElement('accountnum');
            $child = $occ->appendChild($child);
            $value = $doc->createTextNode("$accountnum");
            $value = $child->appendChild($value);

            $child = $doc->createElement('dataareaid');
            $child = $occ->appendChild($child);
            $value = $doc->createTextNode("$dataareaid");
            $value = $child->appendChild($value);

            $child = $doc->createElement('salesinvoicemail');
            $child = $occ->appendChild($child);
            $value = $doc->createTextNode("$salesinvoicemail");
            $value = $child->appendChild($value);

            if ($salesinvoiceerror)
            {
                $child = $doc->createElement('salesinvoiceerror');
                $child = $occ->appendChild($child);
                $value = $doc->createTextNode("$salesinvoiceerror");
                $value = $child->appendChild($value);
            }

            $child = $doc->createElement('accountstatementmail');
            $child = $occ->appendChild($child);
            $value = $doc->createTextNode("$accountstatementmail");
            $value = $child->appendChild($value);

            if ($accountstatementmailerror)
            {
                $child = $doc->createElement('accountstatementmailerror');
                $child = $occ->appendChild($child);
                $value = $doc->createTextNode("$accountstatementmailerror");
                $value = $child->appendChild($value);
            }

            $child = $doc->createElement('salesconfirmmail');
            $child = $occ->appendChild($child);
            $value = $doc->createTextNode("$salesconfirmmail");
            $value = $child->appendChild($value);

            if ($salesconfirmmailerror)
            {
                $child = $doc->createElement('salesconfirmmailerror');
                $child = $occ->appendChild($child);
                $value = $doc->createTextNode("$salesconfirmmailerror");
                $value = $child->appendChild($value);
            }

            $child = $doc->createElement('contactname');
            $child = $occ->appendChild($child);
            $value = $doc->createTextNode("$contactname");
            $value = $child->appendChild($value);


    // get completed xml document
    $xml_string = $doc->saveXML();

    $xml_string = utf8_encode($xml_string);

    if ($salesinvoiceerror || $accountstatementmailerror || $salesconfirmmailerror)
    {
        $myFile = "errors/$accountnum.xml";
        $fh = fopen($myFile, 'w') or die("can't open file");
        fwrite($fh, $xml_string);
        fclose($fh);

        $to = "http://edoc.menu.as/$accountnum/$recid/error";
        header('Location: '. $to);
    }
    else
    {
        $myFile = "outbox/$accountnum.xml";
        $fh = fopen($myFile, 'w') or die("can't open file");
        fwrite($fh, $xml_string);
        fclose($fh);

        $myFile = "archive/$accountnum.xml";
        $fh = fopen($myFile, 'w') or die("can't open file");
        fwrite($fh, $xml_string);
        fclose($fh);

        $myFile = "errors/$accountnum.xml";
        $fh = fopen($myFile, 'w') or die("can't open file");
        fwrite($fh, $xml_string);
        fclose($fh);

        header("Location: http://edoc.menu.as/$accountnum/$recid/thanks");
    }
?>
